package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.constance.Sharep;
import com.typy.www.tpay.util.CheckUtil;
import com.typy.www.tpay.util.CountDownTimerUtils;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PreferencesUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/6/11.
 */

public class ForgetPassWordActivity extends BaseBackActivity {


    @BindView(R.id.et_phonenumber)
    AutoCompleteTextView etPhonenumber;
    @BindView(R.id.et_yanzhengma)
    AutoCompleteTextView etYanzhengma;
    @BindView(R.id.btn_yanzhengma)
    TextView btnYanzhengma;
    @BindView(R.id.et_newPassWord)
    AutoCompleteTextView etNewPassWord;
    @BindView(R.id.btn_queding)
    TextView btnQueding;
    @BindView(R.id.ll_yanzhengma)
    PercentLinearLayout llYanzhengma;
    @BindView(R.id.tv_wangjimima)
    TextView tvWangjimima;
    @BindView(R.id.et_newPayPassWord)
    AutoCompleteTextView etNewPayPassWord;
    @BindView(R.id.et_oldPassWord)
    AutoCompleteTextView etOldPassWord;
    @BindView(R.id.et_oldPayPassWord)
    AutoCompleteTextView etOldPayPassWord;
    private int classType;
    String strPer;

    public static void show(Context context, int type) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        IntentUtil.intent2Activity(context, intent, ForgetPassWordActivity.class);
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_forget;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        classType = getIntent().getIntExtra("type", 0);
        updataTitle();
    }

    private void updataTitle() {
        if (classType == Constance.REFINDPAYPASSWORD) {
            setTitle("找回支付密码");
            tvWangjimima.setVisibility(View.GONE);
            etNewPassWord.setVisibility(View.GONE);
            etNewPayPassWord.setVisibility(View.VISIBLE);
        } else {
            setTitle("忘记密码");
            etNewPassWord.setVisibility(View.VISIBLE);
            etNewPayPassWord.setVisibility(View.GONE);
            tvWangjimima.setVisibility(View.GONE);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
    }

    @OnClick({R.id.btn_yanzhengma, R.id.btn_queding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_yanzhengma:
                if (CheckUtil.checkPhoneEmptyOrTrue(this, etPhonenumber)) {
                    CountDownTimerUtils.getInstant(btnYanzhengma, 60000, 1000).start();
                    doSendMSN();
                }
                break;
            case R.id.btn_queding:
                save();
                break;
        }
    }

    private void save() {
        if (Constance.REFINDPAYPASSWORD == classType) {
            if (CheckUtil.checkText(this, etPhonenumber, etYanzhengma, etNewPayPassWord)) {
                if (CheckUtil.checkPhoneTrue(this, etPhonenumber)&&CheckUtil.checkText(getActivity(),"请输入6位支付密码",6,etNewPayPassWord))
                    doResetPayPassWord();
            }
        } else {
            if (CheckUtil.checkText(this, etPhonenumber, etYanzhengma, etNewPassWord)) {
                if (CheckUtil.checkPhoneTrue(this, etPhonenumber)&&CheckUtil.checkText(getActivity(),"请输入至少6位登录密码",6,etNewPassWord))
                    doResetPassWord();
            }

        }
    }

    private void doResetPayPassWord() {

        ParamsUtil params = new ParamsUtil();
        params.put("fdCode", etPhonenumber);//手机号
        params.putMd5("fdPayPwd", etNewPayPassWord);//md5加密后的密码，都是小写
        params.put("fdValidCode", etYanzhengma);//验证码

        HTTPUtils.post(getActivity(), Constance.getResetpaypwdURL(), params.getParams(), new VolleyListener() {

            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), "支付密码设置成功！", true);
                PreferencesUtil.save(Sharep.FDCODE, etPhonenumber.getText().toString());
                EventBus.getDefault().post(new MessageEvent(MessageAction.REFRESHLOGINNAME));
                finish();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void doResetPassWord() {

        ParamsUtil params = new ParamsUtil();
        params.put("fdCode", etPhonenumber);//手机号
        params.putMd5("fdPwd", etNewPassWord);//md5加密后的密码，都是小写
        params.put("fdValidCode", etYanzhengma);//验证码

        HTTPUtils.post(getActivity(), Constance.getResetpwdURL(), params.getParams(), new VolleyListener() {

            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), "登陆密码设置成功！", true);
                PreferencesUtil.save(Sharep.FDCODE, etPhonenumber.getText().toString());
                EventBus.getDefault().post(new MessageEvent(MessageAction.REFRESHLOGINNAME));
                finish();
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void doSendMSN() {
        ParamsUtil params = new ParamsUtil();
        params.put("fdCode", etPhonenumber);//手机号
        if (Constance.REFINDPAYPASSWORD == classType)
            params.put("fdType", 2);//fdType 0表示注册验证码 1表示忘记密码 2表示修改支付密码
        else
            params.put("fdType", 1);//fdType 0表示注册验证码 1表示忘记密码 2表示修改支付密码
        HTTPUtils.post(getActivity(), Constance.getGetcodeURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                UiHelper.showToast(getActivity(), "验证码发送成功！", true);
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

}
